﻿using Engine;
using Game;
using TemplatesDatabase;

namespace MFTrain
{
    public class SubsystemMFTrainBlockBehavior : SubsystemBlockBehavior
    {
        public SubsystemAudio m_subsystemAudio;

        public SubsystemBodies m_subsystemBodies;

        public SubsystemGameInfo m_subsystemGameInfo;

        public FCSubsystemCreatureSpawn m_creatureSpawn;

        public Engine.Random m_random = new Engine.Random();

        public SubsystemBlockEntities m_subsystemBlockEntities;

        public override int[] HandledBlocks => new int[1] { 544 };

        public override bool OnUse(Ray3 ray, ComponentMiner componentMiner)
        {
            _ = componentMiner.Inventory;
            TerrainRaycastResult? terrainRaycastResult = componentMiner.Raycast<TerrainRaycastResult>(ray, RaycastMode.Digging);
            if (terrainRaycastResult.HasValue && Terrain.ExtractContents(terrainRaycastResult.Value.Value) == 543)
            {
                if (componentMiner.ActiveBlockValue == Terrain.MakeBlockValue(544, 0, 0))
                {
                    Vector3 position = new Vector3(terrainRaycastResult.Value.CellFace.Point) + new Vector3(0.5f, 0f, 0.5f);
                    m_creatureSpawn.FCSpawnCreature("MFTrain", position, constantSpawn: true, componentMiner, terrainRaycastResult);
                    componentMiner.RemoveActiveTool(1);
                    m_subsystemAudio.PlaySound("Audio/BlockPlaced", 1f, 0f, position, 3f, autoDelay: true);
                    return true;
                }

                if (componentMiner.ActiveBlockValue == Terrain.MakeBlockValue(544, 0, 1))
                {
                    TerrainRaycastResult value = terrainRaycastResult.Value;
                    SubsystemTerrain subsystemTerrain = base.Project.FindSubsystem<SubsystemTerrain>();
                    int cellValueFast = subsystemTerrain.Terrain.GetCellValueFast(value.CellFace.X, value.CellFace.Y, value.CellFace.Z);
                    int num = Terrain.ExtractData(cellValueFast);
                    if (Terrain.ExtractContents(cellValueFast) == 543)
                    {
                        if (!MFRailBlock.IsBreaker(num))
                        {
                            subsystemTerrain.ChangeCell(value.CellFace.X, value.CellFace.Y, value.CellFace.Z, Terrain.MakeBlockValue(543, 0, MFRailBlock.SetBreaker(num)));
                        }
                        else
                        {
                            subsystemTerrain.ChangeCell(value.CellFace.X, value.CellFace.Y, value.CellFace.Z, Terrain.MakeBlockValue(543, 0, MFRailBlock.ClearBreaker(num)));
                        }

                        base.Project.FindSubsystem<SubsystemAudio>().PlaySound("Audio/BlockPlaced", 1f, 0f, new Vector3(value.CellFace.X, value.CellFace.Y, value.CellFace.Z), 3f, autoDelay: true);
                        return true;
                    }
                }
            }

            return false;
        }

        public override void Load(ValuesDictionary valuesDictionary)
        {
            base.Load(valuesDictionary);
            m_subsystemAudio = base.Project.FindSubsystem<SubsystemAudio>(throwOnError: true);
            m_subsystemBodies = base.Project.FindSubsystem<SubsystemBodies>(throwOnError: true);
            m_subsystemGameInfo = base.Project.FindSubsystem<SubsystemGameInfo>(throwOnError: true);
            m_subsystemBlockEntities = base.Project.FindSubsystem<SubsystemBlockEntities>(throwOnError: true);
            m_creatureSpawn = base.Project.FindSubsystem<FCSubsystemCreatureSpawn>();
        }
    }
}